'use strict'

// 切换工作目录
process.chdir(__dirname)

const titbit = require('titbit');
const fs = require('fs');
const app = new titbit({
    debug: true,
    maxBody:200000000
});

// mvc可视化
const tbloader = require('titbit-loader');
let tb = new tbloader();
tb.init(app);

// 不校验域名
app.use(async (c, next) => {
    let method = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'];
    c.setHeader('Access-Control-Allow-Origin', '*');
    c.setHeader('Access-Control-Allow-Methods', 'method');
    c.setHeader('Access-Control-Allow-Headers', ['content-type', 'update', 'authorization']);
    await next();
})

// 通过try catch 保证images文件夹存在
// 将前端传递过来的验证码图片把存在imags中
try {
    fs.accessSync('./images');
} catch {
    fs.mkdirSync('./images');
}
app.addService('imagePath', `${__dirname}/images`);

// 保存图片对象：imgname,answer,id
let imgArr=[];
app.addService('imgArr',imgArr);

// 保存id值
let idArr=[];
app.addService('idArr',idArr);

// 读取upload页面
app.get('/upload',async c=>{
    await c.helper.pipe('./upload.html',c.reply);
})

// 读取verify页面
app.get('/verify',async c=>{
    await c.helper.pipe('./verify.html',c.reply);
})


// 运行1234端口
app.run(1234);